import {
  setItem
} from './utils.js'
const mutations = {
  SET_STATE: (state, e = {
    key: "",
    value: ""
  }) => {
    const {
      key,
      value,
      persistence
    } = e
    state[key] = value
    if (key === '$memberList') {
      if (state?.$curMember?.id && Array.isArray(value) && value.length > 0) {
        const curMemberInfo = value.find(item => item.id == state?.$curMember?.id)
        if (curMemberInfo) {
          state.$curMember = curMemberInfo
          setItem('$curMember', curMemberInfo)
        }
      }
    }
    if (persistence) {
      setItem(key, value)
    }
  },
  SET_TICKET: (state, ticket) => {
    state.$bx_auth_ticket = ticket
    uni.setStorageSync('bx_auth_ticket', ticket)
  },
  SET_XHR_NUM(state, num) {
    state.$xhrNum = num
  },
  SET_LOGIN_USER(state, loginUser) {
    state.$loginUser = loginUser
    setItem('$loginUser', loginUser)
  },
  SET_LOGIN_STATE(state, isLogin) {
    state.$isLogin = isLogin
    uni.setStorageSync('isLogin', isLogin)
  },
  SET_GOODS_LIST(state, goodsList) {
    state.$goodsList = goodsList
    setItem('$goodsList', goodsList)
  },
  SET_PUTUP_ORDERS(state, putupOrders) {
    state.$putupOrders = putupOrders
    setItem('$putupOrders', putupOrders)
  },
  SET_MEMBERS(state, $memberList) {
    state.$memberList = $memberList
    setItem('$memberList', $memberList)
    if (state?.$curMember?.id) {
      const curMemberInfo = $memberList.find(item => item.id == state?.$curMember?.id)
      if (curMemberInfo) {
        state.$curMember = curMemberInfo
        setItem('$curMember', curMemberInfo)
      }
    }
  },
  // 设置商品展示风格
  SET_GOODS_VIEW_MODE(state, mode) {
    state.$goodsViewMode = mode;
    setItem('$goodsViewMode', mode)
  },
  // 缓存v2数据
  SET_SRV_COLS(state, data) {
    state.$srvCol.push(data)
    setItem('$srvCol', state.$srvCol)
  },
  DEL_SRV_COLS(state, servceName) {
    let fils = state.$srvCol.filter(item => item.service_name !== servceName)
    state.$srvCol = fils
  },
  SET_LOCK_SCREEN(state, data) {
    state.$lockScreenSettings = data
    setItem('$lockScreenSettings', data)
  },
  SET_LOCK_STATE(state, onLock) {
    state.$onLockScreen = onLock
    setItem('$onLockScreen', state.$onLockScreen)
  },
  SET_ACTIVITES(state, activites) {
    state.$activites = activites
    setItem('$activites', state.$activites)
  },
  SET_ACTIVITY_LIST(state,list){
    state.$activityList = list
  },
  SET_GOODS_PRICE_LIST(state, list) {
    state.$goodsVipPriceList = list;
    setItem('$goodsVipPriceList', state.$goodsVipPriceList)
  },
  SET_USER_MENU(state,list){
    state.$userMenuList = list
    setItem('$userMenuList', list)
  },
}

export default mutations
